Lecture 03: Command Line Interface
Department of Quantitative Theory and Methods
Emory University
09 September, 2024
Credit Dave Kerr
Credit Dave Kerr/Kkchaudhary11
Credit Dave Kerr
Credit Dave Kerr
Terminals are really quite simple - they’re just interfaces
The first thing that a terminal will do is run a shell - a programme we can use to operate the computer
Back to the shell: the shell usually takes input
In interactive mode the shell then returns output
The command line represents what is shown and entered in the terminal. They can be customised (e.g., with colour highlighting) to make interaction more convenient
Why should you use this…
… instead of this?
Speed. Typing is fast: A skilled shell user can manipulate a system at dazzling speeds just using a keyboard. Typing commands is generally much faster than exploring through user interfaces with a mouse.
Power. Both for executing commands and for fixing problems. There are some things you just can’t do in an IDE or GUI. It also avoids memory complications associated with certain applications and/or IDEs.
Reproducibility. Scripting is reproducible, while clicking is not.
Portability. A shell can be used to interface to almost any type of computer, from a mainframe to a Raspberry Pi, in a very similar way. The shell is often the only game in town for high performance computing (interacting with servers and super computers).
Automation. Shells are programmable: Working in the shell allows you to program workflows, that is create scripts to automate time-consuming or repetitive processes.
Become a marketable data scientist. Modern programming is often polyglot. The shell provides a common interface for tooling. Modern solutions are often built to run in containers on Linux. In this environment shell knowledge has become very valuable. In short, the shell is having a renaissance in the age of data science.
The shell tools that we’re going to be using have their roots in the Unix family of operating systems originally developed at Bells Labs in the 1970s.
Besides paying homage, acknowledging the Unix lineage is important because these tools still embody the “Unix philosophy”:
Do One Thing And Do It Well
By pairing and chaining well-designed individual components, we can build powerful and much more complex larger systems.
You can see why the Unix philosophy is also referred to as “minimalist and modular”.
Again, this philosophy is very clearly expressed in the design and functionality of the Unix shell.
Let’s open up our shell!
A convenient way to do this is through VS Code’s built-in Terminal.
Click on the View menu, then Terminal. You can also use the shortcut Ctrl + ` (backtick).
Your system default shell is loaded. To find out what that is, type echo $SHELL in the terminal.
{bash, echo=TRUE} echo $SHELL
It is Z shell in my case
… what about you? It is your turn to find out!
Of course, it’s always possible to open up the shell directly if you prefer. It’s your turn!
Feel free to check our class tutorial on how to set up your shell in VS Code.
Open your terminal and type the following commands (without the $):
{bash, eval=FALSE, echo=TRUE} echo $SHELL whoami pwd mkdir new-folder cd .. ls man ls # type 'j' to scroll down, 'k' to scroll up, 'q' to quit
Share your results with a colleague (or the class)!
You should see something like:
{bash, eval=FALSE, echo=TRUE} username@hostname:~$
This is shell-speak for: “Who am I and where am I?”
username denotes a specific user (one of potentially many on this computer).
@hostname denotes the name of the computer or server.
:~ denotes the directory path (where ~ signifies the user’s home directory).
$ (or maybe %) denotes the start of the command prompt.
#).{bash, echo=TRUE} whoami pwd
All bash commands have the same basic syntax:
command option(s) argument(s)
Examples:
```{bash, eval=FALSE, echo=TRUE} # list files in the Documents directory # with human-readable sizes
ls -lh ~/Documents
<br>
```{bash, eval=FALSE, echo=TRUE}
# sort the file and remove duplicates
sort -u file.txt
Commands
You don’t always need options or arguments
For example:
ls ~/Documents/ and ls -lh ~/Documents are both valid commands that will yield (different) outputHowever, you always need a command.
All Bash commands have the same basic syntax:
command option(s) argument(s)
Examples:
```{bash, eval=FALSE, echo=TRUE} # list files in the Documents directory # with human-readable sizes
ls -lh ~/Documents
<br>
```{bash, eval=FALSE, echo=TRUE}
# sort the file and remove duplicates
sort -u file.txt
Options (also called Flags)
Start with a dash. Usually one letter.
Multiple options can be chained under a single dash.
{bash, eval=FALSE, echo=TRUE} ls -l -a -h /var/log # This works ls -lah /var/log # So does this
An exception is with (rarer) options requiring two dashes.
{bash, eval=FALSE, echo=TRUE} ls --group-directories-first --human-readable /var/log
l: Use a long listing format. This option shows detailed information about the files and directories
h: With -l, print sizes in human-readable format (e.g., KB, MB)
u: Unique, it filters out the duplicate entries in the output
Think it’s difficult to memorize what the individual letters stand for? You’re totally right!
All Bash commands have the same basic syntax:
command option(s) argument(s)
Examples:
{bash, eval=FALSE, echo=TRUE} $ ls -lh ~/Documents/
{bash, eval=FALSE, echo=TRUE} $ sort -u file.txt
Arguments
Tell the command what to operate on.
Totally depends on the command what legit inputs are.
Can be a file, path, a set of files and folders, a string, and more
Sometimes more than just one argument is needed:
{bash, eval=FALSE, echo=TRUE} mv figs/cat.png best-figs/cat02.png
The man tool can be used to look at the manual page for a topic.
The man pages are grouped into sections, we can see them with man man.
The cht.sh website can be used directly from the shell to get help on tools. Run it like this: curl cht.sh/command
manThe man command (“manual pages”) is your friend if you need help.
{bash, echo=TRUE} man ls
manManual pages are shown in the shell. Here are the essentials to navigate through contents presented in the pager:
d - Scroll down half a pageu - Scroll up half a pagej / k - Scroll down or up a line. You can also use the arrow keys for thisq - Quit/pattern - Search for text provided as “pattern”n - When searching, find the next occurrenceN - When searching, find the previous occurrenceman tricks are detailed in the help pages (hit “h” when you’re in the pager for an overview). RTFM!
Always check the documentation!
man page explorer challengePartner up and choose a command from the list below. Use man to complete these tasks:
{bash, eval=FALSE, echo=TRUE} # Choose one: ls, cd, cp, mv, rm, mkdir, rmdir, touch, cat, find
You have about 5 minutes. Be ready to share your findings!
Reflection: How was using man compared to online searches? How might you use it in future projects?
Key navigation commands are: - pwd to print (the current) working directory. - cd to change directory.
{bash, echo=TRUE} pwd
You can use absolute paths, but it’s better to use relative paths and invoke special symbols for a user’s home folder (~), current directory (.), and parent directory (..) as needed
Let’s try it out!
Accessing the figures/ directory:
{bash, echo=TRUE} cd ./figures # Change to the figures directory pwd
Now, let’s go back to the previous directory:
{bash, echo=TRUE} cd ../ # Go back to the previous directory pwd
Back two directories:
{bash, echo=TRUE} cd ../../ # Go back two directories pwd
Beware of directory names that contain spaces. Say you have a directory called “My Documents”. (I’m looking at you, Windows.)
$ cd My Documents work?$ cd "My Documents".$ cd My\ Documents.assignment-05 or assignment_05ls (list): Show files and directories in the current directoryls -l: Long listing format with detailed informationls -a: Show hidden files (those starting with a dot)ls -lh: Long listing format with human-readable sizesls -R: List subdirectories recursivelypwd (print working directory): Show the current directory pathcd (change directory): Change the current working directorycd -: Go back to the previous directory.: Refers to the current directory..: Refers to the parent directory~: Refers to the home directorymkdir: Create a new directorytouch: Create a new empty file or update timestampscp: Copy files or directoriesmv: Move or rename files or directoriesrm: Remove files (use with caution!)rmdir: Remove empty directoriescat: Display file contentsfind: Search for files and directoriesFor a more detailed overview, click here
Follow these steps to practice using basic shell commands. Type each command and observe the results.
cd ~mkdir practice && cd practicetouch file1.txt file2.txtlsmv file2.txt file3.txtls && cd ~rm -r practicelscd ~mkdir shell-practicecd shell-practicetouch file1.txt file2.txt file3.txtlsmkdir subdirmv file2.txt subdir/cp file1.txt file4.txtls -Rcd ..rm -r shell-practicelsBonus: Try using the man command to learn more about any of the commands you’ve used.
pwd, ls, and cd for file system navigation~, ., and .. for directory navigation